package cn.itsuorce.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailUtils {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送邮件
     * @param to
     * @param subject
     * @param text
     */
    public void sendMail(String to, String subject, String text) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            //复杂邮件的工具类  multipart 表示可以上传附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //发件人
            helper.setFrom("1282294480@qq.com");
            //收件人
            helper.setTo(to);
            //抄送
            //helper.setCc("842196809@qq.com");
            //邮件主题
            helper.setSubject(subject);
            //邮件内容 第二个参数 true表示支持解析html标签
            helper.setText(text, true);
            //添加附件
            //helper.addAttachment("1.png", new File("C:\\Users\\Administrator\\Desktop\\1.png"));
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
